package com.afd.ozblip.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.afd.ozblip.activity.R;
import com.afd.ozblip.entity.CouponEntity;
import com.afd.ozblip.helper.CouponVH;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.StringUtil;


public class UserCouponListAdapter extends BaseAdapter {

	public List<CouponEntity> dataList;
	private LayoutInflater inflater;
	private Context context;

	public UserCouponListAdapter(Context paramContext) {
		context = paramContext;
		this.dataList = new ArrayList<CouponEntity>();
		this.inflater = LayoutInflater.from(context);
	}

	/**
	 *获取列表条数
	 */
	@Override
	public int getCount() {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int paramInt) {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.get(paramInt);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int paramInt) {
		return paramInt;
	}

	@Override
	public View getView(int position, View paramView, ViewGroup paramViewGroup) {
		try {
			CouponVH couponVH = null;
			if (paramView == null) {
				couponVH = new CouponVH();
				paramView = this.inflater.inflate(R.layout.user_coupon_list_item,
						null);
				
				couponVH.businessNameTv = (TextView) paramView
						.findViewById(R.id.tv_coupon_item_businessName);
				couponVH.couponTitleTv = (TextView) paramView
						.findViewById(R.id.tv_coupon_item_title);
				couponVH.businessAreaTv = (TextView) paramView
						.findViewById(R.id.tv_coupon_item_area);
				couponVH.businessTypeTv = (TextView) paramView
						.findViewById(R.id.tv_coupon_item_class);
				paramView.setTag(couponVH);
			} else {
				couponVH = (CouponVH) paramView.getTag();
			}
			
			CouponEntity vip = this.dataList.get(position);
			if (vip != null) {
				// 商家名称
				String bName = vip.getBusinessName();
				if (!StringUtil.isNullOrEmpty(bName)) {
					couponVH.businessNameTv.setText(bName);
				}
				// 优惠券标题
				String cTitle = vip.getCouponTitle();
				if (!StringUtil.isNullOrEmpty(cTitle)) {
					couponVH.couponTitleTv.setText(cTitle);
				}
				// 商家地区
				String bArea = vip.getBusinessArea();
				if (!StringUtil.isNullOrEmpty(bArea)) {
					couponVH.businessAreaTv.setText(bArea);
				}
				// 商家类型
				String bClass = vip.getBusinessType();
				if (!StringUtil.isNullOrEmpty(bClass)) {
					couponVH.businessTypeTv.setText(bClass);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramView;
	}

	/**
	 * 添加多项
	 */
	public void addItems(List<CouponEntity> commentList) {
		this.dataList.addAll(commentList);
	}

	/**
	 * 清除所有项
	 */
	public void removeAllItem() {
		this.dataList.clear();
	}
}

